<?php
    $heroID = $_GET["arg1"];
    $s = dibi::query("SELECT COUNT(*) FROM hero WHERE %and", array(array("id = %i", $heroID),
                                                                   array("account = %i", $data["user"]["id"])));
    $kontrola = $s->fetchSingle();

    if (!$kontrola)
    {
        echo html_error(dl_lang_general("msg.data_manipulation"));
    }
    else
    {
        // ------ načtení hrdiny
        $heroD = new Hero($heroID);
        
        // ------- načtení a výpočet statistik
        $s = dibi::query("SELECT statistic, value FROM hero_statistics WHERE %and", array(array("hero = %i", $heroID),
                                                                                          array("(statistic = 'battle-won' OR statistic = 'battle-loose')")));
        $r = $s->fetchAll();
        
        $statistics = array("battle-won"   => 0,
                            "battle-loose" => 0);
        foreach($r AS $v)
        {
            $statistics[$v->statistic] = $v->value;
        }
        
        $battle_count = $statistics["battle-won"] + $statistics["battle-loose"];
        $battle_succ = round(($battle_count/100)*$statistics["battle-won"], 2);
        
        // ------ číselníky
        $races = getInum("RACE");
        
        // ------- inventář
        $inventory = array();
        $armory = array();
        $s = dibi::query("SELECT i.*, ui.id AS uitem, ui.number, (SELECT COUNT(*)
                                                                    FROM hero_person
                                                                   WHERE hero = ui.hero AND item = ui.id) AS used
                            FROM hero_inventory ui, items i
                           WHERE ui.hero = %i", $heroID,
                            "AND ui.item = i.id");
        $r = $s->fetchAll();
        foreach ($r AS $v)
        {
            if ($v->used > 0)
            {
                    $armory[$v->use] = array("uid"   => $v->uitem,
                                             "id"    => $v->id,
                                             "image" => $v->image . "_x" . $v->color . ".png");
            }
            else
            {
                    $inventory[$v->uitem] = array("id"     => $v->id,
                                                  "image"  => $v->image . "_x" . $v->color . ".png",
                                                  "use"    => $v->use,
                                                  "number" => $v->number);
            }
        }
        
        // ------- výpočet životů a many
        $hp_width = round((250/$heroD->hpMax)*$heroD->hp);
        if ($heroD->manaMax > 0)
        {
                $mana_width = round((250/$heroD->manaMax)*$heroD->mana);
        }
        else
        {
                $mana_width = 0;
        }
        
        // ------- výpis
        ?>
        <h1><?php echo dl_lang_general("main.hero_detail"); ?></h1>
        
        <h2 style="text-align: center"><?php echo $heroD->name; ?></h2>

        <h3><?php echo dl_lang_general("main.general_informations"); ?></h3>
        <button class="button-tear" style="float: right" onclick="changeHeroName(<?php echo $heroID; ?>);"><?php echo dl_lang_general("main.change_hero_name"); ?> (5 <span class="button-tear-image"></span>)</button>        
        <table class="table-hero-detail" cellspacing="8">
            <tr>
                <th><?php echo dl_lang_general("main.race"); ?>:</th>
                <td><?php echo $heroD->raceName; ?></td>
            </tr>
            <tr>
                <th><?php echo dl_lang_general("main.level"); ?>:</th>
                <td><?php echo $heroD->level; ?></td>
            </tr>
            <tr>
                <th><?php echo dl_lang_general("main.gold"); ?>:</th>
                <td><?php echo $heroD->gold; ?></td>
            </tr>
            <tr>
                <th><?php echo dl_lang_general("main.experiences"); ?>:</th>
                <td><?php echo $heroD->exp; ?></td>
            </tr>
            <tr>
                <th><?php echo dl_lang_general("main.fought_battles"); ?>:</th>
                <td><?php echo $battle_count . " (".dl_lang_general("main.won").": " . $statistics["battle-won"] . ", ".dl_lang_general("main.success_rate").": " . $battle_succ . "%)"; ?></td>
            </tr>
        </table>
        
        <h3><?php echo dl_lang_general("main.properties"); ?></h3>
        
        <table class="table-hero-detail" style="float: left;" cellspacing="8">
            <tr>
                <th><?php echo dl_lang_general("main.attack"); ?>:</th>
                <td><?php echo $heroD->attackMin . " - " . $heroD->attackMax; ?></td>
            </tr>
            <tr>
                <th><?php echo dl_lang_general("main.strenght"); ?>:</th>
                <td><?php echo $heroD->strength; ?></td>
            </tr>
            <tr>
                <th><?php echo dl_lang_general("main.dexterity"); ?>:</th>
                <td><?php echo $heroD->dexterity; ?></td>
            </tr>
            <tr>
                <th><?php echo dl_lang_general("main.intelligence"); ?>:</th>
                <td><?php echo $heroD->intelligence; ?></td>
            </tr>
            <tr>
                <th><?php echo dl_lang_general("main.vitality"); ?>:</th>
                <td><?php echo $heroD->vitality; ?></td>
            </tr>
            <tr>
                <th><?php echo dl_lang_general("main.wisdom"); ?>:</th>
                <td><?php echo $heroD->wisdom; ?></td>
            </tr>
        </table>

        <div style="float: right; padding-right: 150px;">
            <div class='form-label'><b><?php echo dl_lang_general("main.life"); ?></b></div>
            <div class="health-bar"><div class="health-bar-line" style="width: <?php echo $hp_width; ?>px"><div class="health-bar-title"><?php echo $heroD->hp . "/" . $heroD->hpMax; ?></div></div></div>
            <br />
            <div class='form-label'><b><?php echo dl_lang_general("main.mana"); ?></b></div>
            <div class="mana-bar"><div class="mana-bar-line" style="width: <?php echo $mana_width; ?>px"><div class="mana-bar-title"><?php echo $heroD->mana . "/" . $heroD->manaMax; ?></div></div></div>
        </div>
        
        <div style="clear: both;"></div>
        
        <h3><?php echo dl_lang_general("main.inventory"); ?></h3>
        
        <center>
            <div id="armory">
                <div id="armor_spot" class="spot"><?php echo (isset($armory["armor"]) ? html_item($armory["armor"]["image"], $armory["armor"]["id"], $armory["armor"]["uid"], "armor", "position: relative;") : ''); ?></div>
                <div id="weapon_spot" class="spot"><?php echo (isset($armory["weapon"]) ? html_item($armory["weapon"]["image"], $armory["weapon"]["id"], $armory["weapon"]["uid"], "weapon", "position: relative;") : ''); ?></div>
                <div id="ring_spot" class="spot"><?php echo (isset($armory["ring"]) ? html_item($armory["ring"]["image"], $armory["ring"]["id"], $armory["ring"]["uid"], "ring", "position: relative;") : ''); ?></div>
            </div>

            <div id="inventory">
                <?php
                    foreach ($inventory AS $id => $values)
                    {
                        echo html_item($values["image"], $values["id"], $id, $values["use"], "position: relative;", $values["number"]);
                    }
                ?>
            </div>
        </center>
        <div style="clear: both;"></div>
        <?php
    }